//
//  CGCityViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/5.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGCityViewController.h"

@interface CGCityViewController ()

@property(nonatomic,strong)NSArray *cities;

@end

@implementation CGCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    
    self.title = [NSString stringWithFormat:@"当前省份: %@",self.myProvince.province];
    
    [self initTableView];
    [self fetchAllCity];

    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }

    
}

-(void)fetchAllCity{
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"page"] = @"0";
    parameters[@"provinceId"] = [NSString stringWithFormat:@"%d",self.myProvince.province_id];
    ZYHRequest *request = [ZYHRequest new];
    [request getAsyncEncryptRequest:@"/main/getCity.app?" parameters:parameters success:^(id responseData) {
        
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
       
        if (result.length) {
            self.cities = [CGCity objectArrayWithJSONData:responseData];
        }else{
            self.cities = nil;
        }
        
        //把接收到的数组对象变成model
        
        [self.tableView reloadData];

    } failure:^(NSError *error) {
        
        NSLog(@"城市信息请求失败:%@",error);
    }];
}

#pragma mark overide UITableViewDataSource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"tableviewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    CGCity *city = self.cities[indexPath.row];
    cell.textLabel.text = city.city;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        CGCity *city = self.cities[indexPath.row];
        CGHomePageViewController *homeVC = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 3];
        homeVC.cityName = city.city;
    CGDataCenter *dataCenter = [CGDataCenter sharedDataCenter];
    dataCenter.selectedCity = city.city;
        [self.navigationController popToViewController:homeVC animated:YES];

}

//UITableViewCell左侧会有默认15像素的空白,使cell的线条充满
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}


@end
